package com.orhon.smartcampus.modules.systemctl.mutation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.orhon.smartcampus.modules.systemctl.dao.*;
import com.orhon.smartcampus.modules.systemctl.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLOperationInput;
import com.orhon.smartcampus.modules.systemctl.inputMapper.OperationInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddOperationMutation implements DataFetcher {


    @Autowired
    OperationsDao operationsDao;

    @Autowired
    ModulesDao modulesDao;

    @Autowired
    OperationApiRelationDao operationApiRelationDao;

    @Autowired
    MenusDao menusDao;

    @Autowired
    WidgetsDao widgetsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 新增/修改操作权限
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLOperationInput input = OperationInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Operations operations = null;

        if (input.getId() != null) {
            operations = operationsDao.get(Operations.class, input.getId());
            operations.setUpdatedAt(new Date());
        } else {
            operations = new Operations();
            operations.setCreatedAt(new Date());
        }
        List<OperationApiRelation> operationApiRelationList = new ArrayList<>();
        if(input.getMenus()!=null){
            if (input.getMenus().size() > 0) {
                List<Menus> menuList = new ArrayList<>();
                input.getMenus().forEach(menu -> {
                            try {
                                Menus menus= menusDao.findById(menu);
                                List<Apis> apis = menus.getApis();
                                apis.forEach(api ->{
                                    String api_code = api.getCode();
                                    OperationApiRelation a = new OperationApiRelation();
                                    a.setApi(api_code);
                                    try {
                                        OperationApiRelation newapi = operationApiRelationDao.save(a);
                                        operationApiRelationList.add(newapi);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                } );
                                menuList.add(menus);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                );
                operations.setMenus(menuList);
            }
        }
        if(input.getWidgets()!=null) {
            if (input.getWidgets().size() > 0) {
                List<Widgets> widgetList = new ArrayList<>();
                input.getWidgets().forEach(widget -> {
                            try {
                                Widgets widgets = widgetsDao.findById(widget);
                                List<Apis> apis = widgets.getApis();
                                apis.forEach(api -> {
                                    String api_code = api.getCode();
                                    OperationApiRelation a = new OperationApiRelation();
                                    a.setApi(api_code);
                                    try {
                                        OperationApiRelation newapi = operationApiRelationDao.save(a);
                                        operationApiRelationList.add(newapi);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                });
                                widgetList.add(widgets);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                );
                operations.setWidgets(widgetList);
            }
        }
        if (operationApiRelationList.size() > 0) {
            operations.setOperationApiRelations(operationApiRelationList);
        }
        if (input.getTitle() != null) {
            operations.setTitle(JSON.toJSONString(input.getTitle()));
        }
        if (schoolId != null) {
            operations.setSchoolId(schoolId);
        }
        if (input.getModule_id() != null) {
            operations.setSystemctlModulesByModuleId(modulesDao.get(Modules.class, input.getModule_id()));
        }
        try {
            operationsDao.saveOrUpdate(operations);
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}

